<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>工艺卡复用</h1>
</section>
<section class="content">
    <div class="row">
        <form id="sheetForm" class="form-horizontal" role="form">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">
                    </div>
                    <div class="box-body">
                        <div class="row" style="padding-left: 4%;">
                            <div class="col-lg-6">
                                <h4>被复制项目</h4>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目编号</label>
                                    <div class="col-sm-10">
                                        <select id="proCodeS" class="form-control" name="proCodeS"></select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目名称</label>
                                    <div class="col-sm-10" style="margin-top: 7px;">
                                        <label id="proNameS"></label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目数量</label>
                                    <div class="col-sm-10" style="margin-top: 7px;">
                                        <label id="proQtyS"></label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">工艺卡数量</label>
                                    <div class="col-sm-10" style="margin-top: 7px;">
                                        <label id="cardS"></label>
                                        <input id="cardNumS" type="text" hidden="hidden">
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-6" id="newCon" style="padding-right: 4%;">
                                <h4>复制项目</h4>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目编号</label>
                                    <div class="col-sm-10">
                                        <select id="proCodeC" class="form-control" name="proCodeC"></select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目名称</label>
                                    <div class="col-sm-10" style="margin-top: 7px;">
                                        <label id="proNameC"></label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label no-padding-right">项目数量</label>
                                    <div class="col-sm-10" style="margin-top: 7px;">
                                        <label id="proQtyC"></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div style="margin-top: 20px"
                             shiro:hasPermission="est:copy:edit">
                            <button type="button" id="btn_copy" class="btn btn-success pull-right"> 开始复制</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="help" style="padding-left: 5%;">
                <h3>复制前请确认以下几点：</h3>
                <p>1、被复制的项目全部审核通过；</p>
                <p>2、被复制的项目和复制的项目制作件清单要一致；</p>
                <p>3、复制的项目没有创建过工艺卡；</p>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    $(function () {

        /** 被复制项目 */
        // 项目编号数据绑定
        js.select2({
            id: 'proCodeS',
            placeholder: '',
            ajax: {
                url: function () {
                    return ctx + 'est/copy/projectList'
                },
                processResults: function (data, page) {
                    let array = data;
                    for (let i = 0; i < data.length; i++) {
                        array[i]["id"] = array[i]['code'];
                        array[i]["text"] = array[i]['code'];
                    }
                    return {results: array};
                },
            }
        });
        // 项目编号下拉框选中
        $("#proCodeS").on("select2:select", function () {
            $("#proNameS").text($(this).select2("data")[0].name);
            $("#customerS").text($(this).select2("data")[0].demander);
            $("#insSiteS").text($(this).select2("data")[0].address);
            $("#proQtyS").text($(this).select2("data")[0].num + $(this).select2("data")[0].unit);
            $("#cardS").text($(this).select2("data")[0].cardCount + "张");
            $("#cardNumS").val($(this).select2("data")[0].cardCount);
        });

        /** 复制项目 */
        // 项目编号数据绑定
        js.select2({
            id: 'proCodeC',
            placeholder: '',
            ajax: {
                url: function () {
                    return ctx + 'est/copy/projectList'
                },
                processResults: function (data, page) {
                    let array = data;
                    for (let i = 0; i < data.length; i++) {
                        array[i]["id"] = array[i]['code'];
                        array[i]["text"] = array[i]['code'];
                    }
                    return {results: array};
                },
            }
        });
        // 项目编号下拉框选中
        $("#proCodeC").on("select2:select", function () {
            $("#proNameC").text($(this).select2("data")[0].name);
            $("#customerC").text($(this).select2("data")[0].demander);
            $("#insSiteC").text($(this).select2("data")[0].address);
            $("#proQtyC").text($(this).select2("data")[0].num + $(this).select2("data")[0].unit);
        });

        /** 表单校验 */
        js.validate.init("sheetForm", {
            fields: {
                proCodeS: {validators: {notEmpty: {message: '请选择被复制项目编号'}}},
                proCodeC: {validators: {notEmpty: {message: '请选择复制项目编号'}}}
            }
        });

        /** 开始复制 */
        $("#btn_copy").click(function () {
            if ($("#cardNumS").val() == 0){
                js.modal.warning("被复制项目工艺卡数量为零，不允许复制");
                return false;
            }
            let $this = $(this);

            $this.attr('disabled', true);
            js.validSubmit({
                formId: "sheetForm",
                url: ctx + "est/copy/estCopy",
                data: new FormData($("#sheetForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.removeAttr('disabled');
        })
    })
</script>
</body>
</html>
